package com.jxt.app.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jxt.app.dao.base.ApdialogDAO;
import com.jxt.app.dao.ApreplyDAO;
import com.jxt.app.dao.ApresmemberDAO;
import com.jxt.app.dao.base.ApresourceDAO;
import com.jxt.app.entity.base.Apdialog;
import com.jxt.app.entity.base.ApdialogExample;
import com.jxt.app.entity.base.Apmsgmember;
import com.jxt.app.entity.Apresmember;
import com.jxt.app.entity.base.Apresource;
import com.jxt.app.entity.base.ApresourceExample;
import com.jxt.app.service.ApReplyService;
import com.jxt.app.service.ApResourceService;
import com.jxt.app.util.Page;

@Service
public class ApResourceServiceImpl  implements ApResourceService{
	@Resource
    private ApresourceDAO  apresourceDAO;
	
	
	@Resource
    private ApresmemberDAO  apresmemberDAO;
	
	@Resource
	private  ApdialogDAO apdialogDAO;
	

	@Override
	public int deleteModel(ApresourceExample record) {
		// TODO Auto-generated method stub
		return 0;
	}
	/**
	 * 单表分页
	 */
	@Override
	public Page findListByExample(ApresourceExample inparam, int page, int num)
			throws Exception {
		// TODO Auto-generated method stub
		Page result = new Page();

		try {
			/**
			 * 获取总记录条数
			 */
			int rowcount = apresourceDAO.findCountByExample(inparam);
			/**
			 * 当没有符合查询条件的结果集时
			 */
			if (rowcount <= 0) {
				result.setCurPageNum(1);
				result.setPageCount(1);
				result.setResult(new ArrayList());
				result.setRowCount(0);
				result.setRowCountPerPage(num);
				return result;
			}
			/**
			 * 总页数
			 */
			long pagecount = 0;
			if (rowcount % num == 0) {
				pagecount = rowcount / num;
			} else {
				pagecount = rowcount / num + 1;
			}
			/**
			 * 根据总页数调整当前页码位置
			 */
			if (page <= 1) {
				page = 1;
			} else if (page > pagecount) {
				page = (int) pagecount;
			}

			Integer start = (page - 1) * num;
			Integer end = page * num;

			/**
			 * 获取结果集
			 */
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("object", inparam);
			param.put("page", page);
			param.put("page_begin", start);
			param.put("page_end", end);
			List<?> list = apresourceDAO.findListByExample(param);

			result.setCurPageNum(page);
			result.setPageCount(pagecount);
			result.setResult((ArrayList<?>) list);
			result.setRowCount(rowcount);
			result.setRowCountPerPage(num);

		} catch (Exception e) {
			// logger.error(e);
			throw e;
		}

		return result;
	}

	@Override
	public Apresource getModelById(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int insertModel(Apresource record) {
		// TODO Auto-generated method stub
		int ret=0;
		try{
			long key=apresourceDAO.insert(record);
		
			ret=1;
		}catch(Exception e){
			e.printStackTrace();
		}
		return ret;
	}

	@Override
	public List<Apresource> listModel(ApresourceExample example) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateModel(ApresourceExample record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Apresource> queryUnreadRes(HashMap<String, Object> parms) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 保存资源
	 */

	@Override
	public Apresource save(Apresource record,long userid) throws SQLException {
		// TODO Auto-generated method stub
		ApdialogExample example=new ApdialogExample();//查询会话记录中的发送人和接收人 
		example.setDialogid(record.getDialogid());
		example.setDialogid_Indicator(ApdialogExample.EXAMPLE_EQUALS);
	
	String receiveID="";
    String dialogSendid="";	
	if(record.getDialogid().equals("-1")){//无会话资源
		
		Long key= apresourceDAO.insert(record);//插入资源消息表
		record.setId(key);
	}else{
		
	  List<Apdialog> apdialogList=apdialogDAO.selectByExample(example);
	  if(null!=apdialogList&&apdialogList.size()>0){
		  String dialogid=apdialogList.get(0).getDialogid();
		  receiveID=apdialogList.get(0).getReceiveid();
		  String sendids=apdialogList.get(0).getSendid();
		  dialogSendid=sendids;
		  record.setDialogid(dialogid);
		  long key= apresourceDAO.insert(record);//插入资源消息表
		  record.setId(key);
			long sendid=userid;
			String [] receiveidArray=(receiveID+sendid).split(",");//会话群中所有人包括自己
			List<Apresmember> apresmemberList=new ArrayList<Apresmember>();
			
			
			if(!dialogSendid.equals(String.valueOf(userid))){//是回复消息只需要回复给会话中的主发起人，其他人不可见  
				
				Apresmember apresmemberVo=new Apresmember();
				String ID=UUID.randomUUID().toString().replace("-", "");
				apresmemberVo.setId(ID);
				apresmemberVo.setResid(key);
				apresmemberVo.setSendid(sendid);
				apresmemberVo.setIsread((byte)0);
				apresmemberVo.setIspush((byte)0);
				apresmemberVo.setReceiveid(dialogSendid);//回复给主发起人。即1-->n 中的1
				apresmemberList.add(apresmemberVo);
				
				
			}else{//1->n 发出消息
				
				
				for(int i=0;i<receiveidArray.length;i++){
					
					if(sendid!=Long.parseLong(receiveidArray[i])){//过滤掉自己发给自己的那条消息
						Apresmember apresmemberVo=new Apresmember();
						String ID=UUID.randomUUID().toString().replace("-", "");
						apresmemberVo.setId(ID);
						apresmemberVo.setResid(key);
						apresmemberVo.setSendid(sendid);
						apresmemberVo.setIsread((byte)0);
						apresmemberVo.setIspush((byte)0);
						apresmemberVo.setReceiveid(receiveidArray[i]);
						apresmemberList.add(apresmemberVo);
					}
				}
			}
		  
		  
	  }else{//不保存纪录
		  
		   throw new SQLException();
	  }
	  
	}
	  
	  
	  	

		
	
	  
		//apresmemberDAO.insertBacth(apresmemberList);
		
		return record;
	}
	
	
	/**
	 * 关联分页查询资源
	 */
	@Override
	public Page findListByJoinMap(HashMap<String, Object> parms,int page,int num) {
		// TODO Auto-generated method stub
		Page result = new Page();

		try {
			/**
			 * 获取总记录条数
			 */
			int rowcount = apresourceDAO.findCountByMap(parms);
			/**
			 * 当没有符合查询条件的结果集时
			 */
			if (rowcount <= 0) {
				result.setCurPageNum(1);
				result.setPageCount(1);
				result.setResult(new ArrayList());
				result.setRowCount(0);
				result.setRowCountPerPage(num);
				return result;
			}
			/**
			 * 总页数
			 */
			long pagecount = 0;
			if (rowcount % num == 0) {
				pagecount = rowcount / num;
			} else {
				pagecount = rowcount / num + 1;
			}
			/**
			 * 根据总页数调整当前页码位置
			 */
			if (page <= 1) {
				page = 1;
			} else if (page > pagecount) {
				page = (int) pagecount;
			}

			Integer start = (page - 1) * num;
			Integer end = page * num;

			/**
			 * 获取结果集
			 */
			//HashMap<String, Object> param = new HashMap<String, Object>();
			parms.put("page", page);
			parms.put("page_begin", start);
			parms.put("page_end", end);
			List<?> list = apresourceDAO.findListByMap(parms);
			List<Apresmember> resmList=new  ArrayList<Apresmember>();
			for(Apresource apresource:(List<Apresource>)list){
				Apresmember record=new Apresmember();
				record.setIsread((byte)1);
				record.setIspush((byte)1);
				record.setReceiveid(parms.get("userid").toString());
				record.setResid(apresource.getId());
				resmList.add(record);
				
			}


			result.setCurPageNum(page);
			result.setPageCount(pagecount);
			result.setResult((ArrayList<?>) list);
			result.setRowCount(rowcount);
			result.setRowCountPerPage(num);

		} catch (Exception e) {
			// logger.error(e);
			e.printStackTrace();
		}

		return result;
	}

}
